package cn.iocoder.yudao.module.crm.controller.admin.cluepool;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.crm.controller.admin.cluepool.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.cluepool.CluePoolDO;
import cn.iocoder.yudao.module.crm.service.cluepool.CluePoolService;

@Tag(name = "管理后台 - 线索公海")
@RestController
@RequestMapping("/crm/clue-pool")
@Validated
public class CluePoolController {

    @Resource
    private CluePoolService cluePoolService;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建线索公海")
    @PreAuthorize("@ss.hasPermission('crm:clue-pool:create')")
    public CommonResult<Long> createCluePool(@Valid @RequestBody CluePoolSaveReqVO createReqVO) {
        return success(cluePoolService.createCluePool(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新线索公海")
    @PreAuthorize("@ss.hasPermission('crm:clue-pool:update')")
    public CommonResult<Boolean> updateCluePool(@Valid @RequestBody CluePoolSaveReqVO updateReqVO) {
        cluePoolService.updateCluePool(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线索公海")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:clue-pool:delete')")
    public CommonResult<Boolean> deleteCluePool(@RequestParam("id") Long id) {
        cluePoolService.deleteCluePool(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线索公海")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CluePoolRespVO> getCluePool(@RequestParam("id") Long id) {
        CluePoolDO cluePool = cluePoolService.getCluePool(id);
        return success(BeanUtils.toBean(cluePool, CluePoolRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线索公海分页")
    public CommonResult<PageResult<CluePoolRespVO>> getCluePoolPage(@Valid CluePoolPageReqVO pageReqVO) {
        PageResult<CluePoolDO> pageResult = cluePoolService.getCluePoolPage(pageReqVO);
        PageResult<CluePoolRespVO> result = BeanUtils.toBean(pageResult, CluePoolRespVO.class);
        List<CluePoolRespVO> dataList = result.getList();
        dataList.forEach(resp -> {
            if (resp.getType() == 1) {
                List<Long> idList = StrUtil.split(resp.getDeptIds(), ",", true, true).stream().map(Long::parseLong).collect(Collectors.toList());
                List<DeptRespDTO> deptList = deptApi.getDeptList(idList);
                resp.setDeptName(deptList.stream().map(DeptRespDTO::getName).collect(Collectors.joining(",")));
            } else if (resp.getType() == 2) {
                List<Long> idList = StrUtil.split(resp.getUserIds(), ",", true, true).stream().map(Long::parseLong).collect(Collectors.toList());
                List<AdminUserRespDTO> deptList = adminUserApi.getUserList(idList);
                resp.setUserName(deptList.stream().map(AdminUserRespDTO::getNickname).collect(Collectors.joining(",")));
            }
        });
        result.setList(dataList);
        return success(result);
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得当前用户可用的线索公海")
    @OperateLog(enable = false)
    public CommonResult<List<CluePoolRespVO>> getCluePoolList() {
        List<CluePoolDO> cluePool = cluePoolService.getEnableClueList(getLoginUserId());
        return success(BeanUtils.toBean(cluePool, CluePoolRespVO.class));
    }


}